var DMZ = window.DMZ || {};
DMZ.util = function(){
	return {
		getEle:function(id){
			return ("string" == typeof(id))?document.getElementById(id):id;
		},
		debug:function(s){
				alert(s);
			},//end debug
		testItem:function(o,nois){
			try{
				if(eval(o)){
					(!nois)?DMZ.util.debug(o.toSource()+'\n\n-------------------------\n对象已存在'):null;
					//(!nois)?DMZ.util.debug(o+'\n\n-------------------------\n对象已存在'):null;
					return true;
				}else{
					(!nois)?DMZ.util.debug('对象未定义'):null;
					return false;
				}
			}catch(err){
				(!nois)?DMZ.util.debug(err):null;
				return false;
			}//end try
		}//end testItem
	}// end return
}();
DMZ.hasjQuery = function(){
	return DMZ.util.testItem('jQuery',true);
}();
DMZ.isIE = (document.all)?true:false;
var userAgent = navigator.userAgent.toLowerCase();
DMZ.browser = {
	//version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
	version: (userAgent.match( /.(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
	safari: /webkit/.test( userAgent ),
	opera: /opera/.test( userAgent ),
	msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
DMZ.isIE6 = DMZ.isIE && (DMZ.browser.version == 6.0);
var $ =  DMZ.hasjQuery ? jQuery : DMZ.util.getEle;
DMZ.namespace = function(ns){
	if (!ns || !ns.length) { 
		return null; 
	}
	var nsHandle = DMZ;
	var levels = ns.split('.');
	var levelsLen = levels.length;
		for(var i=(levels[0]=='DMZ') ? 1 : 0; i<levelsLen; ++i){
			nsHandle[levels[i]] = nsHandle[levels[i]] || {};
			//DMZ.util.debug(nsHandle[levels[i]].toSource());
			nsHandle = nsHandle[levels[i]];
			
		}//end for
	return nsHandle;
};

DMZ.namespace('widget');
DMZ.widget = {
	selectCheckbox : function(status,fId){
		/**
		<a href="javascript:void(0);" onclick="DMZ.widget.selectCheckbox(true,'form1');">全选</a>
		<a href="javascript:void(0);" onclick="DMZ.widget.selectCheckbox(false,'form1');">取消选择</a>
		<a href="javascript:void(0);" onclick="DMZ.widget.selectCheckbox('inverse','form1');">反选</a>
		*/
		//var oForm = DMZ.util.getEle(fId).getElementsByTagName('input');
		//var oForm = $('#'+fId)[0].getElementsByTagName('input');
		var oForm = DMZ.hasjQuery?$('#'+fId)[0]:$(fId);
		oForm = oForm.getElementsByTagName('input');
		var checkboxArr = [];
		for(var i = 0, oFLen = oForm.length; i < oFLen; i++){
			if(oForm[i].getAttribute('type').toLowerCase()=='checkbox'){
				checkboxArr.push(oForm[i]);
			}
		}
		checkboxArr.join('');
		if(status!='inverse'){
			for (var i = 0, oFLen = checkboxArr.length; i < oFLen; i++) {
				checkboxArr[i].checked = status;
			}
		}else{
			for (var i = 0, oFLen = checkboxArr.length; i < oFLen; i++) {
				checkboxArr[i].checked?checkboxArr[i].checked = false:checkboxArr[i].checked = true;
			}
		}
	},//end selectCheckbox
	autoCheckbox:function(){
		/**
		<a href="javascript:void(0);" rel="checkbox[true,'form1']">全选</a>
		<a href="javascript:void(0);" rel="checkbox[false,'form1']">取消选择</a>
		<a href="javascript:void(0);" rel="checkbox['inverse','form1']">反选</a>
		*/
		var elA = document.getElementsByTagName('a');
		for(var i = 0,aLen = elA.length;i<aLen;i++){
			if(elA[i].getAttribute('rel')&&elA[i].getAttribute('rel').indexOf('checkbox')!=-1){
				elA[i].onclick = function(){
					var eleArr = eval(this.getAttribute('rel').substr(8));
					//DMZ.util.debug(eleArr[0]);
					DMZ.widget.selectCheckbox(eleArr[0],eleArr[1]);
				};//end click event
				
			}//end if
		}// end for
	},//end autoCheckbox
	cookie:{
		set:function(name,value,option){
			var str=name+"="+escape(value);
				if(option){
					if(option.expireDays){
						var date=new Date();
						var ms=option.expireDays*24*3600*1000;
						date.setTime(date.getTime()+ms);
						str+="; expires="+date.toGMTString();
					}
					if(option.path)str+="; path="+option.path;
					if(option.domain)str+="; domain="+option.domain;
					if(option.secure)str+="; true";
				}
				document.cookie=str;
		},//end set
		read:function(name){
			var cookieArray=document.cookie.split("; ");
			var cookie=new Object();
				for(var i=0;i<cookieArray.length;i++){
					var arr=cookieArray[i].split("=");
					if(arr[0]==name)return unescape(arr[1]);
				}
			return "";
		},//end read
		clear:function(name){
			this.set(name,"",{expireDays:-1});
		},//end clear
		testCookie:function(name){
			DMZ.util.debug(this.read(name));
		}
	},//end cookie
	simpleScroll:function(option){
		var o = DMZ.hasjQuery?($('#'+option.id)[0]):$(option.id);
		var isPause = false;
		var lineheight = option.lineheight||24;
		o.onmouseover = function(){isPause = true;};
		o.onmouseout = function(){isPause = false;};
		window.setInterval(function(){scrollup(o,lineheight,option.interval?(lineheight):0);},(option.time||3000));
		function scrollup(o,d,c){
			if(isPause) return;
			if(d==c){
				var t=getFirstChild(o.firstChild).cloneNode(true);
				o.removeChild(getFirstChild(o.firstChild));
				o.appendChild(t);
				t.style.marginTop="0px";
			}else{
				c+=2;
				getFirstChild(o.firstChild).style.marginTop=-c+"px";
				window.setTimeout(function(){scrollup(o,d,c)},20);
			}
		}
		function getFirstChild(node){
			while (node.nodeType!=1){
				node = node.nextSibling;
			}
			return node;
		}
	},//simpleScroll end
	simpleFade:function(args){
		if(!DMZ.hasjQuery){
			DMZ.util.debug('jQuery库还未载入，simpleFade基于jQuery库运行。');
			return;
		}
		var o = args.id?$('#'+args.id+' ul'):$('.fade ul');	//实际上是拿到列表而不是外围容器
		var time = args.time?args.time:4000;	//停留时间
		var speed = args.speed?args.speed:1000;	//过渡速度
		var mode = args.mode?args.mode:0;	//fade模式
		var isPause = args.stop?true:false;
		var up = function(){
				if(mode!=1){
					return (args.up?true:false);
				}else{
					return false;
				}
			}	//滚动方向,mode==1时无效
			
		//var _width = o.parent().width();
		//var _height = o.parent().height();
		var _$_ = function(){
			if(!up()){
				return (o.parent().width());
			}else{
				return (o.parent().height());
			}
		};
		var iWidth = $(o.children()[0]).width();
		var iHeight = $(o.children()[0]).height();
		var multiple = function(){
				if(!up()){
					return (Math.floor(Math.abs(_$_()/iWidth)));
				}else{
					return (Math.floor(Math.abs(_$_()/iHeight)));
				}
			};
		var _len = o.children().length;
		var i = 0;
		var j = Math.floor(_len/multiple());
		//DMZ.util.debug(multiple());
		var _forward = false;
		//var operate = $('#'+args.id).siblings('p[class=operate]').children('a');
		var operate = $('#'+args.id).siblings('p[class*=operate]');
		if(!operate.is('p')){
			operate = $('#'+args.id).children('p[class*=operate]');
		}
		if(j==1){
			operate.attr('class','').addClass('operate page-turning only-page');
			isPause = true;
			return;
		}
		
		//settings
		if(!up()){
			o.css({
				width:_$_() * _len + 'px',
				height:Math.min(o.parent().height(),o.children().height() ) + 'px',
				position:'absolute'
			});
		}else{
			o.css({
				height:_$_() * _len + 'px',
				position:'absolute'
			});
		}
		
		var setStatus = function(){
			if(i==0){
				operate.attr('class','').addClass('operate page-turning first-page');
			}else if(i==j-1){
				operate.attr('class','').addClass('operate page-turning last-page');
			}else{
				operate.attr('class','').addClass('operate page-turning mid-page');
			}
		};
		
		if(!isPause){
			var interval = window.setInterval(function(){fade.auto()},time);
			
			
			operate.hover(function(){
				//isPause = true;
				window.clearInterval(interval);
			},function(){
				//isPause = false;
				interval = window.setInterval(function(){fade.auto()},time);
			});
			
			o.hover(function(){
				//isPause = true;
				window.clearInterval(interval);
			},function(){
				//isPause = false;
				interval = window.setInterval(function(){fade.auto()},time);
			});
		}
		//change to manually
		operate.children('a').click(function(){
			if($(this).attr('rel')=='prev'&&i==0){
				return false;
			}
			fade.manually($(this).attr('rel'));
		});
		
		var fade = {
			auto:function(){
				//$(o.children()[i]).attr('title',i);	//-debug
				if(mode==1){
					fadeTo();
				}else if(mode==2){
					fadeForward();
				}else{
					fadeBack();
				}
			},//auto scroll
			manually:function(step){
				//$(o.children()[i]).attr('title',i);	//-debug
				if(mode==1){
					fadeTo(step);
				}else if(mode==2){
					fadeForward(step);
				}else{
					fadeBack(step);
				}
			
			}//manual scroll
		};//fade
		
		//fade-1
		var fadeTo = function(s){
			var step = s||'auto';
			if(i==0){
				if(step=='auto'){
					$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){i++;}});
				}else if(step=='next'){
					$(o.children()[0]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i+1]).animate({'opacity':'show'},{duration:speed,complete:function(){i++;}});
					}});
				}
			}else if(i==_len-1){
				//alert(i);
				if(step!='prev'){
					o.children().animate({'opacity':'show'},{duration:speed,complete:function(){i=0;}});
				}else{
					//$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i-1]).animate({'opacity':'show'},{duration:speed,complete:function(){i--;}});
					//}});
				}
			}else{
				if(step!='prev'){
					$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i+1]).animate({'opacity':'show'},{duration:speed,complete:function(){i++;}});
					}});
				}else{
					//$(o.children()[i]).animate({'opacity':'hide'},{duration:speed,complete:function(){
						$(o.children()[i-1]).animate({'opacity':'show'},{duration:speed,complete:function(){i--;}});
					//}});
				}
			}//end out if
		};
		//fade-2
		var fadeForward = function(s){
			var step = s||'auto'; 
			if(i==0){
				_forward = false;
				if(step!='prev'){
					i++;
					up() ? o.animate({'top':-_$_() +'px'},speed) : o.animate({'left':-_$_() +'px'},speed);
				}else{
					return;
				}
				setStatus();
			}else if(i==j-1){
				_forward = true;
				if(step!='next'){
					up() ? o.animate({'top':-_$_()*i+_$_() + 'px'},speed) : o.animate({'left':-_$_()*i+_$_() + 'px'},speed);
					i--;
				}else{
					return;
				}
				setStatus();
			}else{
				if(step=='auto'){
					if(!_forward){
						i++;
						up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
					}else{
						up() ? o.animate({'top':-_$_()*i+_$_() +'px'},speed) : o.animate({'left':-_$_()*i+_$_() +'px'},speed);
						i--;
					}
				}else if(step=='next'){
					i++;
					up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
				}else{
						up() ? o.animate({'top':-_$_()*i+_$_() +'px'},speed) : o.animate({'left':-_$_()*i+_$_() +'px'},speed);
						i--;
				}
				setStatus();
			}
		};//fadeForward
		//fade-0
		var fadeBack = function(s){
			var step = s||'auto';
			if(i==0){
				if(step!='prev'){
					i++;
					up() ? o.animate({'top':-_$_() +'px'},speed) : o.animate({'left':-_$_() +'px'},speed);
				}else{
					return;
				}
				setStatus();
			}else if(i==j-1){
				if(step=='prev'){
					i--;
					up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
				}else{
					i=0;
					up() ? o.animate({'top':0 +'px'},speed) : o.animate({'left':0 +'px'},speed);
				}
				setStatus();
			}else{
				if(step=='prev'){
					i--;
				}else{
					i++;
				}
				up() ? o.animate({'top':-_$_()*i +'px'},speed) : o.animate({'left':-_$_()*i +'px'},speed);
				setStatus();
			}
		};//fadeBack
	},//simpleFade end
	simpleTab:{
		decorate:function(id,data,callback){

			var o = id?'#'+id:DMZ.util.debug('simpleTab(),id不能为空');
			data = data||{};
			var eventType = data.eventType||'click';
			var currentClass = data.currentClass||'selected';
			var index = data.index||0;
			var tabs = $(o + ' > ul');
			var len = tabs.children('li').length;
			var tabSection = $(o + ' > div');
			var sectLen = tabSection.length;
			
			tabSection.not($(tabSection[index])).hide();
			
			$(tabs.children('li')[index]).addClass(currentClass).siblings('li').removeClass(currentClass)
			tabs.children('li').bind(eventType, {'eventType':eventType,'currentClass':currentClass,'tabs':tabs,'tabSection':tabSection,'callback':callback}, DMZ.widget.simpleTab.switchTab);
		},
		switchTab:function(d){
			//$(d.data.tabSection[]);
			var currentIdx = d.data.tabs.children('li').index(this);
			d.data.tabSection.not($(d.data.tabSection[currentIdx]).show()).hide();
			$(this).addClass(d.data.currentClass).siblings('li').removeClass(d.data.currentClass);
			if(d.data.callback){
				d.data.callback();
			}
		},
		switchSpTab:function(d){
			var currentClass = d.currentClass||'selected';
			var index = d.index||0;
			var tabs = $('#'+d.tid + ' > ul');
			var tabSection = $('#'+d.sid + ' > ul');
//DMZ.widget.simpleTab.switchSpTab({tid:'sunflower',sid:'news',index:0});
			var sectLen = tabSection.length;
			if(index < sectLen)
			{
				tabSection.not($(tabSection[index]).show()).hide();
				$(tabs.children('li')[index]).addClass(currentClass).siblings('li').removeClass(currentClass);
				if(d.callback){
					d.callback();
				}
			}
		}
	}//simpleTab end
};//end widget
DMZ.namespace('expand');
DMZ.expand = {
	loginForm:function(args){
		if(!DMZ.hasjQuery){
			DMZ.util.debug('jQuery库还未载入，该应用基于jQuery库运行');
			return;	
		}
		var count = 0,clear;
		var o = args.id?('#'+args.id):'#login';
		
		var bindEvent = function(){
			
			
			$(o+' input[type=text]').add('input[type=password]').focus(function(){
				$(this).css({
					'background-position':'0 20px'
				});
			}).blur(function(){
				if($.trim($(this).val())==''){
					$(this).removeAttr('style');
				}
			});
		};//end bindEvent()
		
		var rebindEvent = function(){
			bindEvent();
			if(count>9){
				clearInterval(clear);
			}else{
				count++;
			}
		};
		if(!DMZ.util.testItem($(o),true)){
			bindEvent();
		}else{
			clear = setInterval(rebindEvent,60);
		}
	},//loginForm end
	pageBar:function(){
		var oPage = $('.page');
		var pageLen = oPage.length;
		var oLi;
		var len = 0;
		var total = 0;
		for(var i=0;i<pageLen;i++){
			oLi = oPage.eq(i).children('li');
			len = oLi.length;
			total = 0;
			for(var j = 0; j<len; j++){
				total += oLi.eq(j).width();
			}
			oPage.eq(i).css('width',total);
		}
	},//pageBar end
	showFlashObject:function(objID,objSource,objWidth,objHeight,objQuality,objWmode,objBgcolor,objXML){
		if(objXML){
		var chkMovie=0;
		if(objSource.match(/=/))objSource=objSource+"&server="+objXML+"&chkMovie="+chkMovie;
		else objSource=objSource+"?server="+objXML+"&chkMovie="+chkMovie;
		}
		var pageUrl=self.window.location.href;
		if(pageUrl.substring(0,5)=="https") swfUrl="https";
		else swfUrl="http";
		if(!objID)objID="ShockwaveFlash1";
		if(!objWidth)objWidth="0";
		if(!objHeight)objHeight="0";
		if(!objQuality)objQuality="high";
		if(!objWmode)objWmode="transparent";
		var strFlv = [];
		strFlv.push('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+swfUrl+'://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0" width="'+objWidth+'" height="'+objHeight+'" id="'+objID+'" align="middle">');
		strFlv.push('<param name="allowFullScreen" value="false" />');
		strFlv.push('<param name="scale" value="noscale" />');
		strFlv.push('<param name="movie" value="'+objSource+'" />');
		strFlv.push('<param name="allowScriptAccess" value="always" />');
		strFlv.push('<param name="menu" value="false" />');
		strFlv.push('<param name="quality" value="high" />');
		strFlv.push('<param name="bgcolor" value="'+objBgcolor+'" />');
		strFlv.push('<param name="wmode" value="'+objWmode+'" />');
		strFlv.push('<embed src="'+objSource+'" allowScriptAccess="always" menu="false" quality="high" bgcolor="'+objBgcolor+'" wmode="'+objWmode+'" width="'+objWidth+'" height="'+objHeight+'" name="'+objID+'" align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
		strFlv.push('</object>');
		return strFlv.join('');
		
	},//showFlashObject end
	shadeLayer:{
		init:function(id){
			var _this = this;
			this.shadeBox = '';
			//this._top = this._left = 0;
			$('select').css('visibility','hidden');
			$('#'+id+' select').css('visibility','visible'); //显示弹窗上的select,modify by Helen,2011-11-23 11:23:41
			//检测shadeBox是否在DOM里
			if(!DMZ.util.testItem(DMZ.util.getEle('shadeBox'),true)){
				this.create();
				this.normal(id);
			}else{
				this.normal(id);
			}
			/*this.center();
			$(window).resize(function(){_this.center();});
			$(window).scroll(function(){_this.center();});*/

			// this.drag();
		},
		create:function(){
			this.shadeBox = [];
			this.shadeBox.push('<div id="shadeBox">');
			this.shadeBox.push('	<div id="alphaBox"></div>');
			this.shadeBox.push('</div>');
			this.shadeBox.join('');
			this.shadeBox = $(this.shadeBox.join('')).appendTo('body');
		},
		normal:function(id){
			var fillBox = $('#'+id);
			this.shadeBox = $('#shadeBox');
			this.shadeBox.show();
			if(DMZ.isIE6){
				var praItem = this.shadeBox.parent();
				$(praItem).css('overflow','hidden');
				this.shadeBox.css({
					height:$(praItem).height()+'px'
				});
				this.setScroll();
				$(window).scroll(function(){DMZ.expand.shadeLayer.setScroll();});
				$(window).resize(function(){DMZ.expand.shadeLayer.setScroll();});
			}
			fillBox.show().css({
				'position':'relative',
				'top':'180px',
				'z-index':1002,
				'margin':'0 auto'
			}).appendTo(this.shadeBox);
		},
		closed:function(callback){
			this.shadeBox.hide();
			//隐藏并转移到body中，2012-1-17 14:39:43
			this.shadeBox.children('.popupbox').hide();
			this.shadeBox.children('.popupbox').appendTo('body');
			$('select').css('visibility','visible');
			if(callback){
				callback();
			}
		},
		setScroll:function(){
			this.shadeBox.css({
					top : $(document).scrollTop() + "px",
					left : $(document).scrollLeft() + "px"
			});
		},
		center : function(){
			var oDiv = this.shadeBox.children('.popupbox')[0];
			var oH = oDiv.offsetHeight;
			var oW = oDiv.offsetWidth;
			var viewH = document.documentElement.clientHeight;
			var viewW = document.documentElement.clientWidth;
			oDiv.style.top = (viewH - oH)/2 + 'px';
			oDiv.style.left = (viewW - oW)/2 + 'px';
		},
		drag : function(){
			var oDiv = this.shadeBox.children('.popupbox')[0];
			oDiv.onmousedown = function(e){
				var ev = e || event;
				var disX = ev.clientX - oDiv.offsetLeft;
				var disY = ev.clientY - oDiv.offsetTop;

				if ( oDiv.setCapture ) {
					oDiv.setCapture();
				}
				document.onmousemove = function(e){
					var ev = e || event;
					var L = ev.clientX - disX;
					var T = ev.clientY - disY;
					//范围限制
					var maxL = document.documentElement.clientWidth - oDiv.offsetWidth;
					var maxT = document.documentElement.clientHeight - oDiv.offsetHeight;
					L = (L >= maxL)? maxL : (L <= 0)? 0 : L;
					T = (T >= maxT)? maxT : (T <= 0)? 0 : T;
					oDiv.style.left = L + 'px';
					oDiv.style.top = T + 'px';
				}

				document.onmouseup = function(){
					document.onmousemove = document.onmouseup = null;
					//释放全局捕获 releaseCapture();
					if ( oDiv.releaseCapture ) {
						oDiv.releaseCapture();
					}
				}
				return false;
			}
			//元素阻止冒泡
			var aP = oDiv.getElementsByTagName('p');
			for( var i=0; i<aP.length; i++ ){
				aP[i].onmousedown = function(e){
					var ev = e || event;
					ev.cancelBubble=true;
				}
			}
			var aInput = oDiv.getElementsByTagName('input');
			for( var i=0; i<aInput.length; i++ ){
				aInput[i].onmousedown = function(e){
					var ev = e || event;
					ev.cancelBubble=true;
				}
			}
		}
	}//shadeLayer end
};//end expand


function dmzgamebanner(myurl, mywidth, myheight, isscroll) {
	//alert("mywidth="+mywidth+"- myheight="+myheight+"- isscroll="+isscroll+"- myurl="+myurl);
	if(mywidth == "0" && myheight == "0") {
		if(isscroll == "0" ) {
			document.location.href= myurl;
		} else {
			window.open(myurl,"_blank");
		}
	} else {
		open_popup(myurl,mywidth,myheight,isscroll,"lolgamebanner" );
	}
}

if(DMZ.isIE6){
	document.execCommand("BackgroundImageCache", false, true);
}